<template>
  <!-- <div class="home-contianer" style="padding-left: 0px;padding-top: 0px;padding-bottom:0px">
    <div>
      <div class="order-title">
        <h2>快捷菜单</h2>
      </div>
      <div data-v-542f4644  style="padding: 15px; background: white;display: flex;
      flex-wrap: wrap;
      width: 100%;">
        <div v-for="item in topMenu" :key="item.MenuName" class="ivu-col ivu-col-span-6"
          style="padding-left: 8px;padding-right: 8px;padding-top: 8px;  width:16.6%;"  @click="openUrl(item)">
          <div data-v-542f4644 class="ivu-card" :style="{ background: item.Color }">
            <div class="icon-left">
              <i class="el-icon-wallet" />
            </div>
            <div class="ivu-card-body">
              <div class="demo-color-desc">{{ item.MenuName }}</div>
            </div>
          </div>
        </div> -->
        <!-- 触摸屏 -->
        <!-- <div class="ivu-col ivu-col-span-6"
          style="padding-left: 8px;padding-right: 8px;padding-top: 8px;  width:16.6%; cursor: pointer;" 
          @click="touchScreenChange">
          <div data-v-542f4644 class="ivu-card" :style="{ background: '#409EFF' }">
            <div class="icon-left">
              <i class="el-icon-wallet" />
            </div>
            <div class="ivu-card-body">
              <div class="demo-color-desc">{{ !isTouchScreen ? '触摸屏' : '网页版' }}</div>
            </div>
          </div>
        </div> -->
      <!-- </div>
    </div>
  </div> -->
  <!-- <iframe v-if="iframeSrc!=''" :src="iframeSrc" frameborder="0" scrolling="auto" id="bi_iframe" @load="adjustIframe"></iframe> -->
  <div class="home-contianer">
    <div>
      <div class="order-title">
        <h2>数量统计</h2>
      </div>
      <div data-v-542f4644 class="ivu-row" style="padding: 15px; background: white">
        <div v-for="item in topColor" :key="item.ItemName" class="ivu-col ivu-col-span-6"
          style="padding-left: 8px; padding-right: 8px">
          <div data-v-542f4644 class="ivu-card" :style="{ background: item.Background }">
            <div class="icon-left">
              <i class="el-icon-wallet" />
            </div>
            <div class="ivu-card-body">
              <div class="demo-color-name">{{ item.ItemName }}</div>
              <div class="demo-color-desc">{{ item.Qty }}</div>
            </div>
          </div>
        </div>
        <div class="ivu-col ivu-col-span-6"
          style="padding-left: 8px; padding-right: 8px">
          <div data-v-542f4644 class="ivu-card" :style="{ background: '#E6A23C' }">
            <div class="icon-left">
              <i class="el-icon-wallet" />
            </div>
            <div class="ivu-card-body">
              <div class="demo-color-name">合格率</div>
              <div class="demo-color-desc">{{ passRate }}%</div>
            </div>
          </div>
        </div>
      </div>
      <div class="order-title">
        <h2>工序信息</h2>
      </div>
      <div class="order-range">
        <div class="order-item" v-for="(item, index) in totalRange" :key="index">
          <div class="total">
            <div class="number">
              {{item.processName}}
            </div>
          </div>
          <div class="date">
            报工数:{{item.reportQty}}
          </div>
          <div class="date">
            良品数:{{item.goodQty}}
          </div>
          <div class="date">
            不良品数:{{item.noGoodQty}}
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="home-contianer">

    <div class="h-chart">
      <div id="h-chart2"></div>
      <div id="h-chart3"></div>
    </div>
  </div>
</template>
<script>
  // import * as echarts from "echarts";
  import "echarts/lib/chart/bar";
  import "echarts/lib/chart/line";

  import "echarts/lib/chart/pie";
  import "echarts/lib/component/legend";
  import "echarts/lib/component/tooltip";
  import "echarts/lib/component/title";
  import "echarts/lib/component/grid";
  let echarts = require("echarts/lib/echarts");
  import { chart2, chart3 } from "./home/home-chart-options";
  import { ref, onMounted, onUnmounted } from "vue";
import { mapState, mapMutations } from 'vuex'
  var $chart2;
  export default {
    components: {},
    data() {
      return {
        beginDate: "",
        endDate: "",
        iframeHeight:0,
        topMenu: [],
        topColor: [],
        totalRange: [],
        passRate: 0,
        titleLeft: "",
        dateNow: "",
        iframeSrc: "",
        n: 90,
        value1: "1",
      };
    },
    computed: {
      ...mapState(['isTouchScreen'])
    },
    created() {
      this.getDesktop();
      this.getDate();
      this.getDesktopMenu();
      this.getTopNumber();
      this.getTopProcessNumber();
    },
    mounted() {
      this.initCharts();
    },
    methods: {
      ...mapMutations(['setIsTouchScreen']),
      initCharts() {
        // 使用 $nextTick 确保 DOM 已经渲染
        this.$nextTick(() => {
          this.initChart2();
          this.initChart3();
        });
      },
      initChart2() {
        this.http.get('api/ReportManage/GetProductReceiveInfo/GetProductReceiveInfo', {}, true).then((result) => {
          if(result.ok&&result.data&&result.data.length>0) {
            let categories = result.data.map(item => item.inventoryName);
            let data = result.data.map(item => item.quantity);
            chart2.xAxis[0].data = categories;
            chart2.series[0].data = data;
            $chart2 = echarts.init(document.getElementById("h-chart2"));
            $chart2.setOption(chart2);
            // 添加窗口大小变化时的自适应
            window.addEventListener('resize', () => {
              $chart2 && $chart2.resize();
            });
          }
        });
      },
      initChart3() {
        this.http.get('api/ReportManage/GetDefectItemInfoList/GetDefectItemInfoList', {}, true).then((item) => {
          if(item.ok&&item.data&&item.data.length>0) {
            let dataDefect = item.data.map(key => ({ name: key.defectItemName, value: key.quantity }));
            chart3.series[0].data = dataDefect;
            $chart3 = echarts.init(document.getElementById("h-chart3"));
            $chart3.setOption(chart3);
            // 添加窗口大小变化时的自适应
            window.addEventListener('resize', () => {
              $chart3 && $chart3.resize();
            });
          }
        });
      },
      touchScreenChange () {
        this.setIsTouchScreen(!this.isTouchScreen)
      },
      getDesktopMenu() {
        let urlWo = 'api/Base_DesktopMenu/getDesktopMenu';
        //给工序名称重新绑定数据源
        this.http.get(urlWo, {}, true).then((result) => {
          this.topMenu = result;
        });
      },
      openUrl(item) {
        this.$tabs.open({
          text: item.MenuName,
          path: item.MenuUrl,
          query: {}
        });
      },
      adjustIframe() {
        var ifm = document.getElementById("bi_iframe");
        ifm.height = this.iframeHeight = document.documentElement.clientHeight;
        ifm.width = document.documentElement.clientWidth - 200;
      },
      getDesktop() {
        let url = "api/Bi_desktop/getDesktop"
        this.http.get(url, {}, true).then(rows => {
          if (rows.length > 0) {
            //vtype=1为隐藏标题
            this.iframeSrc = this.http.ipAddress + "BiManage/AppPage/DATABI/YBPVIEW.html?ID=" + rows[0].ViewID + "&token=" + this.$store.getters.getToken() + "&vtype=1";
          }
        })
      },
      getDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minutes = date.getMinutes();
        var second = date.getSeconds();
        this.beginDate =
          year +
          "-" +
          (month < 10 ? "0" + month : month) +
          "-" +
          (day < 10 ? "0" + day : day);
        this.endDate = this.beginDate;
        this.dateNow = this.beginDate;
      },
      getTopNumber() {
        let urlWo = 'api/ReportManage/GetQuantityStatisticsInfo/GetQuantityStatisticsInfo';
        //给工序名称重新绑定数据源
        this.http.get(urlWo, {}, true).then((result) => {
          if(result.ok&&result.data) {
            this.passRate = Math.floor( result.data.qualifiedQuantity / result.data.inspectionQuantity * 100)
            let index = 0
            const obj = {
              inspectionQuantity: { name: '质检数量', Background: '#409EFF' },
              qualifiedQuantity: { name:'合格数', Background: '#67C23A' },
              unQualifiedQuantity: { name:'不合格数', Background: '#F56C6C' }
            }
            for(let key in result.data) {
              this.topColor[index] = {
                Background: obj[key].Background,
                ItemName: obj[key].name,
                Qty: result.data[key] || 0
              }
              index++
            }
          }
        });
      },
      getTopProcessNumber() {
        let urlWo = 'api/ReportManage/GetProcessInfo/GetProcessInfo';
        //给工序名称重新绑定数据源
        this.http.get(urlWo, {}, true).then((result) => {
          console.log(result);
          if(result.ok&&result.data&&result.data.length>0) {
            this.totalRange = result.data
          }
        });
      },
    },
    setup() {
      let open = (item) => {
        window.open(item.url, "_blank");
      };
      let interval;
      onMounted(() => {
      });
      onUnmounted(() => {
        interval && clearInterval(interval);
        // 销毁所有图表实例
        if ($chart2) {
          $chart2.dispose();
          $chart2 = null;
        }
        if ($chart3) {
          $chart3.dispose();
          $chart3 = null;
        }
        // 移除事件监听器
        window.removeEventListener('resize', () => {
          $chart2 && $chart2.resize();
          $chart3 && $chart3.resize();
        });
      });
      return { open };
    },
    destroyed() {
      $chart2 = null;
    },
  };
  var $chart, $chart2, $chart3;

</script>
<style lang="less" scoped>
  .home-contianer {
    padding: 6px;
    background: #eee;
    width: 100%;
    height: 100%;
    // max-width: 800px;
    // position: absolute;
    top: 0;
    right: 0;
    left: 0;
    margin: 0 auto;

    .h-top {
      display: flex;

      .h-top-left {
        height: 100%;
        width: 300px;
        background: white;
      }

      height: 300px;
    }

    .h-top>div {
      border: 1px solid #e8e7e7;
      border-radius: 5px;
      // margin: 6px;
    }

    .h-top-center {
      height: 100%;
      background: white;
      margin: 0 6px;
      display: flex;
      flex-direction: column;

      .item1 .num {
        padding-top: 28px;
      }

      .item2 .num {
        padding-bottom: 20px;
      }

      .n-item {
        width: 100%;
        height: 100%;
        text-align: center;
        cursor: pointer;

        // display: flex;
        .item {
          border-right: 1px solid #e5e5e5;
          width: 33.3333333%;
          float: left;
          height: 50%;
          border-bottom: 1px solid #e5e5e5;
          padding: 47px 0;
          font-size: 13px;
        }

        .item:hover {
          background: #f9f9f9;
          cursor: pointer;
        }

        .item:last-child {
          border-right: 0;
        }

        .item3,
        .item6 {
          border-right: 0;
        }

        .num {
          word-break: break-all;
          color: #282727;
          font-size: 30px;
          transition: transform 0.8s;
        }

        .num:hover {
          color: #55ce80;
          transform: scale(1.2);
        }

        .text {
          font-size: 13px;
          color: #777;
        }
      }
    }

    .h-top-right {
      // flex: 1;

      width: 20%;
      height: 100%;
      background: white;
    }

    .h3 {
      padding: 7px 15px;
      font-weight: 500;
      background: #fff;
      border-bottom: 1px dotted #d4d4d4;
    }
  }

  .task-table {
    table {
      width: 100%;

      .thead {
        font-weight: bold;
      }

      tr {
        cursor: pointer;

        td {
          border-bottom: 1px solid #f3f3f3;
          padding: 9px 8px;
          font-size: 12px;
        }
      }

      tr:hover {
        background: #eee;
      }
    }
  }

  .h-chart {
    height: 340px;
    margin: 6px 0px;
    display: flex;

    .h-left-grid {
      width: 16%;
      height: 100%;
      background: white;
      display: inline-block;

      .name {
        margin-left: 40px;
      }

      .item:hover {
        background: #f9f9f9;
        cursor: pointer;
      }

      .item {
        padding: 30px 30px;
        float: left;
        width: 99%;
        height: 33.33333%;
        border-bottom: 1px solid #eee;
        border-right: 1px solid #eee;

        i {
          font-size: 30px;
        }

        .desc {
          font-size: 12px;
          color: #2b2525;
          padding: 0px 0 0 40px;
          line-height: 1.5;
          margin-top: -10px;
        }
      }
    }
  }

  #h-chart2 {
    border-radius: 3px;
    background: white;
    padding-top: 10px;
    height: 100%;
    width: 0;
    flex: 1;
    margin: 0 7px;
  }

  #h-chart3 {
    border-radius: 3px;
    padding: 10px 10px 0 10px;
    background: white;
    // padding-top: 10px;
    height: 100%;

    width: 600px;
  }

  .ivu-card-body {
    text-align: center;
    padding: 20px 5px;
    /* padding-left: 80px; */
    font-size: 16px;
  }

  .demo-color-name {
    color: #fff;
    font-size: 14px;
  }

  .demo-color-desc {
    color: white;
    /* opacity: 0.7; */
    font-size: 20px;
    margin-top: 2px;
  }

  .ivu-card {
    box-shadow: 0 3px 13px rgba(117, 114, 114, 0.47);
    display: flex;
    position: relative;
    padding-top: 10px;
    border-radius: 5px;
  }

  .ivu-card .icon-left {
    width: 85px;
  }

  .ivu-card .ivu-card-body {
    flex: 1;
  }

  .ivu-card .icon-left {
    text-align: center;
    border-right: 1px solid;
    padding: 8px 0px;
    height: 100%;

    font-size: 50px;
    color: white;
  }

  .ivu-row {
    border-bottom: 2px dotted #eee;
    padding: 15px;
    margin-bottom: 15px;
    display: flex;
  }

  .ivu-row>div {
    flex: 1;
  }

  .h5-desc {
    padding-top: 10px;
  }
</style>

<style lang="less" scoped>
  .jn-day-total {
    display: flex;
    padding: 15px;
    background: white;

    .date-text {
      line-height: 36px;
      padding: 0 15px;
    }

    .date {
      margin-right: 20px;
    }

    .btn {
      margin-left: 10px;
    }
  }

  .order-title {
    h2 {
      padding: 7px 15px;
      font-weight: 500;
      background: white;
      border-bottom: 1px dotted #d4d4d4;
    }
  }

  .order-range {
    padding: 0 15px 10px;
    background: white;
    background: white;
    display: flex;
    // flex-direction: row-reverse;
  }

  .order-range .order-item {
    box-shadow: 0 3px 13px rgba(117, 114, 114, 0.47);
    flex: 1;
    border-radius: 6px;
    font-size: 14px;
    text-align: center;
    border: 1px solid #e6e6e6;
    margin: 7px;
  }

  .order-range .total {
    color: white;
    font-size: 30px;
    font-weight: bold;
    line-height: 60px;
    background: #55ce80;
    font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
      "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  }

  .order-range .number {
    transition: transform 0.8s;
  }

  .order-range .number:hover {
    cursor: pointer;
    transform: scale(1.2);
  }

  .order-range .name {
    font-size: 20px;
    padding: 10px;
  }

  .order-range .date {
    padding: 1px 0 5px 0;
    color: #282727;
    font-size: 15px;
  }
</style>


<style lang="less" scoped>
  .numbers {
    margin-bottom: 15px;
    border-radius: 5px;
    border: 1px solid #eaeaea;
    background: white;
    display: flex;

    padding: 20px 0px;

    .item {
      flex: 1;
      text-align: center;
      border-right: 1px solid #e5e5e5;
    }

    .item>div:first-child {
      word-break: break-all;
      color: #282727;
      font-size: 30px;
      // padding-bottom: 12px;
    }

    .item>div:last-child {
      font-size: 13px;
      color: #777;
    }

    .item:last-child {
      border-right: none;
    }

    .number {
      cursor: pointer;
      transition: transform 0.8s;
    }

    .number:hover {
      transform: scale(1.2);
      color: #03c10b !important;
    }
  }
</style>